<?php

namespace app\home\controller\content;

use app\common\controller\Jwt;
use app\home\model\Article as ArticleModel;

/**
 * 文章管理控制器
 * Class article
 * @package app\admin\controller\content
 */
class Article extends Jwt
{
    /**
     * 构造方法
     * @access public
     * @param  App  $app  应用对象
     */
    public function initialize()
    {
        parent::initialize();
        // 控制器初始化
        $this->model = new ArticleModel();
    }
    /**
     * 文章列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function list()
    {
        return $this->renderSuccess($this->model->getList($this->request->param(),'article_id,article_title,image_id,category_id,actual_views,virtual_views,create_time'));
    }

    /**
     * 添加文章
     * @return array|mixed
     */
    public function add()
    {
        // 新增记录
        if ($this->model->add($this->request->only(['article_title','show_type', 'category_id','image_id','article_content','article_sort','article_status','virtual_views','actual_views']))) {
            return $this->renderSuccess([],'添加成功');
        }
        return $this->renderError($this->model->getError() ?? '添加失败');
    }

    /**
     * 文章详细
     * @param $article_id
     * @return array|mixed
     * @throws \think\exception\DbException
     */
    public function detail()
    {
        // 更新记录
        if ($data = $this->model->detail($this->request->param('article_id'))) {
            return $this->renderSuccess($data);
        }
        return $this->renderError($this->model->getError() ?? '更新失败');
    }

    /**
     * 编辑文章
     * @param $article_id
     * @return array|mixed
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        // 更新记录
        if ($this->model->edit($this->request->only(['article_id','article_title','show_type', 'category_id','image_id','article_content','article_sort','article_status','virtual_views','actual_views']))) {
            return $this->renderSuccess([],'编辑成功');
        }
        return $this->renderError($this->model->getError() ?? '更新失败');
    }

    /**
     * 删除文章
     * @param $article_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function delete()
    {
        if (!$this->model->setDelete($this->request->param('article_id'))) {
            return $this->renderError($this->model->getError() ?? '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

}